
#include "Show3d.h"


CShow3d::CShow3d()
{
	m_blaster_anim[0] = 0;  // blinking animation
	m_blaster_anim[1] = 1;  // blinking animation
	m_blaster_anim[2] = 2;  // blinking animation
	m_blaster_anim[3] = 1;  // blinking animation
	m_blaster_anim[4] = 0;  // blinking animation
}
	
CShow3d::~CShow3d()
{

}

int CShow3d::Game_Init()
{
	// this function is where you do all the initialization 
	// for your game

	int index; // looping var

	// start up DirectDraw (replace the parms as you desire)
	int nRet = m_ddraw.Init(m_hWnd, m_WndWidth, m_WndHeight, m_bpp, m_bFullScreen);
	if (nRet != 0)
		return-1;

	m_graphGdi.Init(&m_ddraw);

	// initialize directinput
	m_gameInput.Init(m_hInstance, m_hWnd);

	// acquire the keyboard 
	m_gameInput.Init_Keyboard();

	m_gameInput.Init_Joystick(-24,24,-24,24);

	m_gameInput.Init_Mouse();

	// initialize directsound and directmusic
	m_gameSound.Init(m_hWnd);
	m_gameMusic.Init(m_hWnd, m_gameSound.GetDSound());

	// hide the mouse
	ShowCursor(FALSE);

	// seed random number generator
	srand(m_gameClock.Start_Clock());

	// load the background
	m_bitMapFile.LoadBitMapFile("MUSH.BMP");

	// set the palette to background image palette
	m_ddraw.Set_Palette(m_bitMapFile.GetBitMap()->palette);

	// load in the four frames of the mushroom
	for (index=0; index<4; index++)
	{
		// create mushroom bitmaps
		m_mushRooms[index].CreateImage(0,0,32,32);

		m_mushRooms[index].Load_Image_Bitmap(&m_bitMapFile,index,0,BITMAP_EXTRACT_MODE_CELL);  
	} // end for index

	// now create the bug blaster bob
	m_blaster.Init(m_ddraw.GetDDraw());

	m_blaster.Create(0,0,32,32, 
		0, 0, m_WndWidth-1, m_WndHeight-1, 
		3,
		BOB_ATTR_VISIBLE | BOB_ATTR_MULTI_ANIM | BOB_ATTR_ANIM_ONE_SHOT,
		DDSCAPS_SYSTEMMEMORY);

	// load in the four frames of the mushroom
	for (index=0; index<3; index++)
		m_blaster.Load_Frame(m_bitMapFile.GetBitMap(), index, index, 1, BITMAP_EXTRACT_MODE_CELL);  


	// set the animation sequences for bug blaster
	m_blaster.Load_Animation(0,5,m_blaster_anim);

	// set up stating state of bug blaster
	m_blaster.SetPos(320, 400);
	m_blaster.SetAnimSpeed(3);

	// set clipping rectangle to screen extents so objects dont
	// mess up at edges
	//RECT screen_rect = {0,0,screen_width,screen_height};
	//lpddclipper = DDraw_Attach_Clipper(lpddsback,1,&screen_rect);

	// create mushroom playfield bitmap
	m_playField.CreateImage(0,0, m_WndWidth, m_WndHeight);
	m_playField.GetImage()->attr |= BITMAP_ATTR_LOADED;

	// fill in the background
	m_bitMapFile.LoadBitMapFile("GRASS.BMP");

	// load the grass bitmap image
	m_playField.Load_Image_Bitmap(&m_bitMapFile,0,0,BITMAP_EXTRACT_MODE_ABS);

	// create the random mushroom patch
	for (index=0; index<50; index++)
	{
		// select a mushroom
		int mush = rand()%4;

		// set mushroom to random position
		m_mushRooms[mush].GetImage()->x = rand()%(m_WndWidth-32);
		m_mushRooms[mush].GetImage()->y = rand()%(m_WndHeight-128);

		// now draw the mushroom into playfield
		m_mushRooms[mush].Draw_BitMap(m_playField.GetImage()->buffer, m_playField.GetImage()->width,1);
	} // end for

	// return success
	return 0;

} // end Game_Init


int	CShow3d::Game_Main()
{
	// this is the workhorse of your game it will be called
	// continuously in real-time this is like main() in C
	// all the calls for you game go here!

	int          index;             // looping var
	int          dx,dy;             // general deltas used in collision detection


	// start the timing clock
	m_gameClock.Start_Clock();

	// clear the drawing surface
	m_ddraw.Fill_Surface(0, BACK_SURFACE);

	// get the joystick data
	//m_gameInput.Read_Joystick();
	m_gameInput.Read_Mouse();

	// lock the back buffer
	m_ddraw.Lock_Back_Surface();

	// draw the background reactor image
	m_playField.Draw_BitMap(m_ddraw.GetBackBuffer(), m_ddraw.GetBackLpitch(), 0);

	// unlock the back buffer
	m_ddraw.Unlock_Back_Surface();

	DIMOUSESTATE* pMouseState = m_gameInput.GetMouseState();
	m_blaster.Move(pMouseState->lX, pMouseState->lY);
	//m_blaster.Move(1, -1);


	// is player firing?
	RECT clipRect = {0, 0, m_WndWidth-1, m_WndHeight-1};
	if (pMouseState->rgbButtons[0])
		m_missile.Start(m_blaster.GetPosX(), m_blaster.GetPosY(), clipRect);
		//m_missile.Start(100, m_blaster.GetPosY(), clipRect);


	//// move and draw missle
	m_missile.Move(0, -10);
	m_missile.Draw(&m_ddraw);

	//CGraphLine	m_line;
	//m_ddraw.Lock_Back_Surface();
	//m_line.Init(m_ddraw.GetBackBuffer(), m_ddraw.GetBackLpitch(), clipRect);

	//// draw the missile in green
	//m_line.Draw_Line(m_blaster.GetPosX(), m_blaster.GetPosY(),
	//	m_blaster.GetPosX(), m_blaster.GetPosY()+6,
	//	250);
	//m_ddraw.Unlock_Back_Surface();

	// is it time to blink eyes
	if ((rand()%100)==50)
		m_blaster.SetAnimation(0);

	// draw blaster
	m_blaster.Animate();
	m_blaster.Draw(m_ddraw.GetBackSurface());

	// draw some text
	m_graphGdi.Draw_Text("Make My Centipede!",0,0,RGB(255,255,255));

	// display joystick and buttons 0-7
	//sprintf(buffer,"Joystick Stats: X-Axis=%d, Y-Axis=%d, buttons(%d,%d,%d,%d,%d,%d,%d,%d)",
	//	joy_state.lX,joy_state.lY,
	//	joy_state.rgbButtons[0],
	//	joy_state.rgbButtons[1],
	//	joy_state.rgbButtons[2],
	//	joy_state.rgbButtons[3],
	//	joy_state.rgbButtons[4],
	//	joy_state.rgbButtons[5],
	//	joy_state.rgbButtons[6],
	//	joy_state.rgbButtons[7]);

	//display joystick and buttons 0-7
	char buffer[256] = {0};
	sprintf(buffer,"Mouse Stats: X-Axis=%d, Y-Axis=%d, buttons(%d,%d,%d)",
		pMouseState->lX,pMouseState->lY,
		pMouseState->rgbButtons[0],
		pMouseState->rgbButtons[1],
		pMouseState->rgbButtons[2]);

	m_graphGdi.Draw_Text(buffer,0,m_WndHeight-40,RGB(255,255,50));

	//// print out name of joystick
	sprintf(buffer, "Blaster: X %d,  Y %d",m_blaster.GetPosX(), m_blaster.GetPosY());
	m_graphGdi.Draw_Text(buffer,0,m_WndHeight-60,RGB(255,255,50));;


	// flip the surfaces
	m_ddraw.Flip(m_Window_client_x0, m_Window_client_y0);

	// sync to 30 fps
	m_gameClock.Wait_Clock(30);


	// check of user is trying to exit
	if (KEY_DOWN(VK_ESCAPE) || m_gameInput.GetKeyState(DIK_ESCAPE))
	{
		PostMessage(m_hWnd, WM_DESTROY,0,0);

	} // end if

	return 0;
}

int	CShow3d::Game_Shutdown()
{
	// release all your resources created for the game here....
	// kill the bug blaster
	m_blaster.Destroy();

	// kill the mushroom maker
	for (int index=0; index<4; index++)
		m_mushRooms[index].DestroyImage();
	
	// kill the playfield bitmap
	m_playField.DestroyImage();

	//// now directsound
	//DSound_Stop_All_Sounds();
	//DSound_Delete_All_Sounds();
	//DSound_Shutdown();

	//// directmusic
	//DMusic_Delete_All_MIDI();
	//DMusic_Shutdown();

	CGameMain::Game_Shutdown();

	return 0;
}
